/*
 * @Author: caiqi
 * @Date: 2019-02-14 14:34:34
 * @Last Modified by: cai.qi
 * @Last Modified time: 2020-05-15 11:52:28
 * @Describe:
 */

"use strict";

import { Service } from "egg";
function createNonceStr() {
    return Math.random().toString(36).substr(2, 15)
}
export default class TypeService extends Service {
    // ------------------------------ 新
    async create(body: any) {
        const { ctx } = this;
        // const type = await ctx.model.ModCoupon.findOne({ where: { name: body.name, status: 1 } });
        // if (type) {
        //     return ctx.throw('该品类已存在', 201);
        // }
        console.log('===============', body)
        body.code = createNonceStr()
        const data = await ctx.model.ModCoupon.create(body);
        return data;
    }
    // ------------------------------ 获取所有品类
    async get(body: any) {
        const { ctx } = this;
        // const _body = body;
        console.log(body)
        const data = await ctx.model.ModCoupon.findAll({
            where: { status: 1 },
            // ASC:升序(默认),DESC:降序。
            order: [[['created_at', 'DESC']]],
        });
        return data;
    }
    async findOne(id) {
        const { ctx } = this;
        const data = await ctx.model.ModCoupon.findOne({ where: { status: 1, id }, })
        return data
    }
    async updata(id: any, body: any) {
        const { ctx } = this;
        const type = await ctx.model.ModCoupon.findOne({ where: { id } });

        if (type) {
            const data = await type.update(body);
            return data;
        } else {
            return ctx.throw('无此ID', 201);
        }
    }
    async del(id: any) {
        const { ctx } = this;
        const type = await ctx.model.ModCoupon.findOne({ where: { id } });

        if (type) {
            await type.update({ ...type, status: 0 });
            return {};
        } else {
            return ctx.throw('无此ID', 201);
        }
    }

    // ------------------------------ 用户获取优惠券
    async user_create(_code: any, openid: any) {
        const { ctx } = this;
        const coupon = await ctx.model.ModCoupon.findOne({ where: { code: _code, status: 1 } });
        if (!coupon) {
            return ctx.throw('优惠券不存在', 201);
        }
        const body = {
            one_key: coupon.id,
            openid,
            type: 'coupon'
        }
        const data = await ctx.model.ModSandbox.create(body);
        return data;
    }
    // ------------------------------ 获取所有品类
    async user_get(_openid) {
        const { ctx, app } = this;
        // const _body = body;
        const sequelize = app.Sequelize;

        console.log('===============', _openid)
        const data = await ctx.model.ModSandbox.findAll({
            where: { openid: _openid, status: 1 },
            order: [['created_at', 'DESC']],
            attributes: [
                'id',
                // 'introduction', 'introduce', 'price', 'oldprice',
                // 'cover', 'collection',
                // 'created_at', 'folder',
                // [sequelize.col('type.id'), 'type_id'],
                
                [sequelize.col('coupon.deductible_amount'), 'deductible_amount'],
                [sequelize.col('coupon.name'), 'name'],
                [sequelize.col('coupon.introduction'), 'introduction'],
                [sequelize.col('coupon.start_time'), 'start_time'],
                [sequelize.col('coupon.end_time'), 'end_time']
            ],
            include: [
                {
                    model: app.model.ModCoupon,
                    as: 'coupon',
                    // 指定关联表查询属性，这里表示不需要任何关联表的属性
                    attributes: [],
                    // through: {
                    //     // 指定中间表的属性，这里表示不需要任何中间表的属性
                    //     attributes: [['project.projectName', 'projectName']],
                    // }
                },
            ],

            raw: true,
            // 这个需要和上面的中间表属性配合，表示不忽略include属性中的attributes参数
            includeIgnoreAttributes: true,
        });
        console.log('===============', data)
        return data;
    }

    // ------------------------------ 用户使用优惠券
    async user_use(_id: string) {
        const { ctx } = this;
        const data = await ctx.model.ModSandbox.findOne({ where: { id: _id } });
        if (data) {
            await data.update({ ...data, status: 0 });
            return {};
        } else {
            return ctx.throw('无此ID', 201);
        }
    }

}
